package mbp.tests;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import mbp.agents.MbpManagerAgent;
import mbp.base.MessageType;
import mbp.base.ResourceType;
import mbp.base.ServiceType;
import mbp.core.BasicUUID;
import mbp.core.JarFolder;
import mbp.core.RandObjectBuilder;
import mbp.core.Resource;
import mbp.core.ResourceManager;
import mbp.core.ResourceTypeBuilder;
import mbp.core.ResourceUtility;
import mbp.core.ResourceValidation;
import mbp.core.ServiceTypeBuilder;
import mbp.etypes.Currency;
import mbp.etypes.Fuel;
import mbp.exceptions.InvalidEnumTypeException;
import mbp.exceptions.InvalidFeatureNameException;
import mbp.exceptions.InvalidFeatureTypeException;
import mbp.exceptions.InvalidFeatureValueException;
import mbp.exceptions.InvalidResourceTypeException;
import mbp.exceptions.InvalidValueException;
import mbp.exceptions.ParseExpressionErrorException;
import mbp.gui.frames.ContentViever;
import mbp.gui.frames.MbpManagerGui;
import mbp.message.Copier;
import mbp.message.Message;
import mbp.message.MessageXml;

public class ConsoleTest 
{
	/**
	 * @param args
	 * @throws InvalidEnumTypeException 
	 * @throws InvalidFeatureNameException 
	 * @throws InvalidFeatureValueException 
	 * @throws InvalidFeatureTypeException 
	 * @throws InvalidResourceTypeException 
	 * @throws ParseExpressionErrorException 
	 * @throws InvalidResourceTypeNameException 
	 */
	public static void main(String[] args) throws InvalidFeatureNameException, InvalidEnumTypeException, InvalidFeatureTypeException, InvalidFeatureValueException, InvalidResourceTypeException, ParseExpressionErrorException 
	{
		JarFolder jf = new JarFolder();
		PropertyConfigurator.configure(jf.getJarFolder() + "conf" + File.separatorChar + "log4j.properties");
		Logger logger = Logger.getLogger("mbp.tests.ConsoleTest");
		//logger.
		logger.info("Sarted main");
		/**
		 * Utworzenie i inicjalizacja zasobu
		 */
		ResourceTypeBuilder rtb = new ResourceTypeBuilder("");
		ResourceType resourceType = rtb.GetResourceType("TESTER");
		Resource Tester = new Resource(resourceType);		
		try {
			Tester.setOwner("WojciechW");
			Date d = new Date();
			Tester.setValidDate(d);
			
			Tester.setFeatureValue("JAKOSC", 2);
			Tester.setFeatureValue("FUEL", "DIESEL");
			Tester.setFeatureValue("MONEY", "CHF");
			Tester.setFeatureValue("CENA", Float.parseFloat("1.12"));
			Tester.setFeatureValue("MASA", Float.parseFloat("33.234"));
		} catch (InvalidFeatureNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFeatureTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFeatureValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Tester.setQuantity(12);
		} catch (InvalidValueException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Tester.printResourceInfo();
		
		ResourceValidation rv = new ResourceValidation(resourceType);
		rv.setCondition( "{JAKOSC>=2}&&( {FUEL!=DIESEL}||( {CENA<1.01}||{MASA>=12.002} ) )" );
		rv.validateCondition(Tester);
		try {
			System.out.println("RESULT: " + rv.validateCondition(Tester));
		} catch (ParseExpressionErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//ServiceTypeBuilder stb = new ServiceTypeBuilder("");
		//stb.GetServiceType("service2");
		mbp.message.Message mm = new Message(MessageType.RESOURCE, "source", "dest");
		
		
		
		
		
		/*
		XmlTests xmlt = new XmlTests();
		String xmlStr = xmlt.ResourceToXmlStr(Tester, "TESTER[0]");
		System.out.println(xmlStr);
		
		Resource rc2 = xmlt.XmlStrToResource(xmlStr);
		
		Tester.PrintResourceInfo();
		rc2.PrintResourceInfo();
		System.out.println(MessageType.RESOURCE.toString());
		MessageType messageType = MessageType.valueOf("RESOURCE");
		System.out.println(messageType.toString());
		*/
		/*
		Date dt = new Date();
		Random r = new Random(); 
		long a = 0x01010101;// * dt.getTime();
		BasicUUID bu = new BasicUUID(a,dt.getTime(),r.nextInt());
		System.out.println(bu.toString());
		System.out.println(bu.toANSIidentifier());
		System.out.println(bu.toHexString());
		BasicUUID bu2 = new BasicUUID(bu.toString());
		System.out.println(bu2.toString());
		System.out.println(bu2.toANSIidentifier());
		System.out.println(bu2.toHexString());
		*/
		//long a = 0x7fffffff;
		//long b = 0x7fffffff;
		//long c = a*b;
		//System.out.println(a);
		//System.out.println(b);
		//System.out.println(c);
		
		//TEST FORMATEK -------------------------------------------------------------
		//ResourceAgentGui rga = new ResourceAgentGui(new ResourceAgent());
		//rga.setVisible(true);
		
		MbpManagerGui mmg = new MbpManagerGui(new MbpManagerAgent());
		mmg.setVisible(true);
		
		//ContentViever cv = new ContentViever("type", "content");
		//cv.setVisible(true);
		
		//ResourcesStorageGui rsg = new ResourcesStorageGui(new ResourcesStorage());
		//rsg.setVisible(true);
		
		//AgentClientGui acg = new AgentClientGui(null);
		//acg.setVisible(true);
		//----------------------------------------------------------------------------
		
		//Integracja -- test
		//AgentTaskTest att = new AgentTaskTest(null);
		//att.process();
		//att.process2();
		
		//OfferState ofs = new OfferState("testing");
		//ofs.putService(0, 23);
		//ofs.putService(2, 48);
		//ofs.putService(3, 112);
		//ofs.putService(5, 2);
		//ofs.putService(8, 234);
		//ofs.putService(11, 44);
		//ofs.putService(14, 11);
		//ofs.putService(15, 50);
		//ofs.putService(16, 50);
		//ofs.putService(20, 50);
		
		//ofs.getSortedList();
		
		//--------TEST LOSOWANIA-----------
//		ResourceType rcType = rtb.GetResourceType("HOUSE");
//		Resource rcHouse = new Resource(rcType);
//		rcHouse.setFeatureValue("FLOOR", 3);
//		rcHouse.setFeatureValue("SIZE", Float.parseFloat("220"));
//		
//		ResourceValidation rcvHouse = new ResourceValidation(rcType);
//		rcvHouse.setCondition("{FLOOR=2}&&({SIZE>200.0}&&{SIZE<300.0})");
//		
//		RandObjectBuilder rd = new RandObjectBuilder();
//		List<Resource> rcList = rd.getResourceList(rcvHouse, rcHouse, 10);
//		//List<Resource> rcList = rd.randomResource(new ResourceValidation(rcType));
//		
//		for(int i = 0; i<rcList.size(); i++)
//		{
//			rcList.get(i).setValidDate(new Date());
//			rcList.get(i).printResourceInfo();
//		}
		
		//--------TEST Funkcji dzielacych-----------
		
		Resource tester2 = Copier.cloneResource(Tester);
		Tester.printResourceInfo();
		tester2.printResourceInfo();
		
		//tester2.setOwner("franek");
		//tester2.getFeatureValue("CENA", 0);
		
		ResourceManager rcm = new ResourceManager();
		
		ResourceManager.mergeResources(Tester, tester2);
		Tester.printResourceInfo();
		tester2.printResourceInfo();
		
		tester2 = null;
		
		try {
			tester2 = ResourceManager.divideResource(Tester, 10);
		} catch (InvalidValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Tester.printResourceInfo();
		tester2.printResourceInfo();
		
		List<Resource> rcList = new ArrayList<Resource>();
		rcList.add(Tester);
		rcList.add(null);
		rcList.add(tester2);
		System.out.println("rcListSize: " + rcList.size());
		rcList.get(0).printResourceInfo();
		rcList.get(2).printResourceInfo();
		
		
		
		//System.out.println("merge condition: " + rcm.checkCondition(Tester, tester2));
		
		///rcm.checkCondition(Tester, tester2);

	}

}
